package com.w7vm.menuweb.managerBean;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.w7vm.menuweb.entity.CardapioBuffetWeb;
import com.w7vm.menuweb.entity.CardapioEspecialidadeWeb;
import com.w7vm.menuweb.entity.CardapioInfantilWeb;
import com.w7vm.menuweb.entity.CardapioLaCarteWeb;
import com.w7vm.menuweb.entity.CardapioSelfServiceWeb;
import com.w7vm.menuweb.facade.CardapioG1Facade;
import com.w7vm.menuweb.facade.CardapioG2Facade;
import com.w7vm.menuweb.facade.CardapioG3Facade;
import com.w7vm.menuweb.service.CardapioService;
import com.w7vm.menuweb.utility.Constantes;
import com.w7vm.menuweb.utility.FacesUtil;
import com.w7vm.menuweb.vo.CardapioVO;

@ManagedBean
@ViewScoped
public class CardapiosConsultaMB  implements Serializable{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@EJB
	private CardapioG1Facade cardapioG1EJB;
	@EJB
	private CardapioG2Facade cardapioG2EJB;
	@EJB
	private CardapioG3Facade cardapioG3EJB;

	CardapioService cardService = new CardapioService();
	
	private LoginMB loginOjb = new LoginMB();
	Constantes constObj = new Constantes();
	
	private CardapioVO cardapioObjVO;
	
	private boolean statusAlterar = true;
	private String tipoCardapio;


	public List<CardapioVO> cardapiosL = new ArrayList<CardapioVO>();
	


// =====================================  METHOD FIND BY CARDAPIOG1 ===================================
	
		public List<CardapioVO> listaCardapioBuffet(){
			List<CardapioVO> cardapioG1L = null;
			List<CardapioBuffetWeb> cardapioBuffetL = null;
			
			int id = 0;
			if(loginOjb.getSessao("AcessoFilial").equals(constObj.ACESSO_FILIAL_SIM)){
				id = (Integer) loginOjb.getSessao("IDRestaurante");
				cardapioBuffetL = cardapioG1EJB.findBuffetByIDRestauranteFilial(id);
				System.out.println("listaCardapioBuffet()................"+ cardapioBuffetL.size());
				if(cardapioBuffetL.size() > 0){
					cardapioG1L = cardService.convertCardapioBuffetEntity(cardapioBuffetL);
				}
			}
			else if(loginOjb.getSessao("AcessoFilial").equals(constObj.ACESSO_FILIAL_NAO)){
				id = (Integer) loginOjb.getSessao("IDSede");
				cardapioBuffetL = cardapioG1EJB.findBuffetByIDRestauranteSede(id);
				if(cardapioBuffetL.size() > 0){
					cardapioG1L = cardService.convertCardapioBuffetEntity(cardapioBuffetL);
				}
			}
			return cardapioG1L;
		}
		
		public List<CardapioVO> listaCardapioSelfService(){
			List<CardapioVO> cardapioG1L = null;
			List<CardapioSelfServiceWeb> cardapioSelfServiceL = null;
			
			int id = 0;
			if(loginOjb.getSessao("AcessoFilial").equals(constObj.ACESSO_FILIAL_SIM)){
				id = (Integer) loginOjb.getSessao("IDRestaurante");
				cardapioSelfServiceL = cardapioG1EJB.findSelfServiceByIDRestauranteFilial(id);
				if(cardapioSelfServiceL.size() > 0){
					cardapioG1L = cardService.convertCardapioSelfServiceEntity(cardapioSelfServiceL);
				}
			}
			else if(loginOjb.getSessao("AcessoFilial").equals(constObj.ACESSO_FILIAL_NAO)){
				id = (Integer) loginOjb.getSessao("IDSede");
				cardapioSelfServiceL = cardapioG1EJB.findSelfServiceByIDRestauranteSede(id);
				if(cardapioSelfServiceL.size() > 0){
					cardapioG1L = cardService.convertCardapioSelfServiceEntity(cardapioSelfServiceL);
				}
			}
			return cardapioG1L;
		}
		
		
// =====================================  METHOD FIND BY CARDAPIOG2 ===================================
		
		public List<CardapioVO> listaCardapioLaCarte(){
			List<CardapioVO> cardapioG2L = null;
			List<CardapioLaCarteWeb> cardapioLaCarteL = null;
			
			int id = 0;
			if(loginOjb.getSessao("AcessoFilial").equals(constObj.ACESSO_FILIAL_SIM)){
				id = (Integer) loginOjb.getSessao("IDRestaurante");
				cardapioLaCarteL = cardapioG2EJB.findLaCarteByIDRestauranteFilial(id);
				if(cardapioLaCarteL.size() > 0){
					cardapioG2L = cardService.convertCardapioLaCarteEntity(cardapioLaCarteL);
				}
			}
			else if(loginOjb.getSessao("AcessoFilial").equals(constObj.ACESSO_FILIAL_NAO)){
				id = (Integer) loginOjb.getSessao("IDSede");
				cardapioLaCarteL = cardapioG2EJB.findLaCarteByIDRestauranteSede(id);
				if(cardapioLaCarteL.size() > 0){
					cardapioG2L = cardService.convertCardapioLaCarteEntity(cardapioLaCarteL);
				}
			}
			return cardapioG2L;
		}
		
		public List<CardapioVO> listaCardapioInfantil(){
			List<CardapioVO> cardapioG2L = null;
			List<CardapioInfantilWeb> cardapioInfantilL = null;
			
			int id = 0;
			if(loginOjb.getSessao("AcessoFilial").equals(constObj.ACESSO_FILIAL_SIM)){
				id = (Integer) loginOjb.getSessao("IDRestaurante");
				cardapioInfantilL = cardapioG2EJB.findInfantilByIDRestauranteFilial(id);
				if(cardapioInfantilL.size() > 0){
					cardapioG2L = cardService.convertCardapioInfantilEntity(cardapioInfantilL);
				}
			}
			else if(loginOjb.getSessao("AcessoFilial").equals(constObj.ACESSO_FILIAL_NAO)){
				id = (Integer) loginOjb.getSessao("IDSede");
				cardapioInfantilL = cardapioG2EJB.findInfantilByIDRestauranteSede(id);
				if(cardapioInfantilL.size() > 0){
					cardapioG2L =  cardService.convertCardapioInfantilEntity(cardapioInfantilL);
				}
			}
			return cardapioG2L;
		}


		

// =====================================  METHOD FIND BY ESPECIALIDADE ===================================
		
		public List<CardapioVO> listaCardapioEspecialidade(){
			List<CardapioVO> cardapioG3L = null;
			List<CardapioEspecialidadeWeb> cardapioEspecialidadeL = null;
			
			int id = 0;
			if(loginOjb.getSessao("AcessoFilial").equals(constObj.ACESSO_FILIAL_SIM)){
				id = (Integer) loginOjb.getSessao("IDRestaurante");
				cardapioEspecialidadeL = cardapioG3EJB.findEspecialidadeByIDRestauranteFilial(id);
				if(cardapioEspecialidadeL.size() > 0){
					cardapioG3L = cardService.convertCardapioEspecialidadeEntity(cardapioEspecialidadeL);
				}
			}
			else if(loginOjb.getSessao("AcessoFilial").equals(constObj.ACESSO_FILIAL_NAO)){
				id = (Integer) loginOjb.getSessao("IDSede");
				cardapioEspecialidadeL = cardapioG3EJB.findEspecialidadeByIDRestauranteSede(id);
				if(cardapioEspecialidadeL.size() > 0){
					cardapioG3L = cardService.convertCardapioEspecialidadeEntity(cardapioEspecialidadeL);
				}
			}
			return cardapioG3L;
		}

		
// =====================================  METHOD FIND BY CARDAPIOS ===================================

		public List<CardapioVO> getCardapiosL() {
			List<CardapioVO> cardBuffetL = null;
			List<CardapioVO> cardSelfServiceL = null;
			List<CardapioVO> cardLaCarteL = null;
			List<CardapioVO> cardEspecialidadeL = null;
			List<CardapioVO> cardInfantilL = null;
			
			cardBuffetL = listaCardapioBuffet();
			System.out.println("TAMANHO DA LIST cardBuffetL.............."+ cardBuffetL);
			if(cardBuffetL != null){
				cardapiosL.addAll(cardBuffetL);
				System.out.println("TAMANHO DA LIST cardBuffetL.............."+ cardapiosL.size());
			}
				
			cardSelfServiceL = listaCardapioSelfService();
			if(cardSelfServiceL != null){
				cardapiosL.addAll(cardSelfServiceL);
			}
				
			cardLaCarteL = listaCardapioLaCarte();
			if(cardLaCarteL != null){
				cardapiosL.addAll(cardLaCarteL);
			}

			cardEspecialidadeL = listaCardapioEspecialidade();
			if(cardEspecialidadeL != null){
				cardapiosL.addAll(cardEspecialidadeL);
			}
			
			cardInfantilL = listaCardapioInfantil();
			if(cardInfantilL != null){
				cardapiosL.addAll(cardInfantilL);
			}
			
			
			return cardapiosL;
		}

		public void setCardapiosL(List<CardapioVO> cardapiosL) {
			this.cardapiosL = cardapiosL;
		}
		
	
// ==================== METHOD REDIRECT -- ALTERAR ===========================


		// CardapioG1 : Buffet | SelfService
		public void redirecionaCardapioAlterar(){
			
			System.out.println("redirecionaCardapioAlterar     "+ cardapioObjVO.getCategoriaCardapio());
			
			if((cardapioObjVO.getCategoriaCardapio().equals(constObj.CARDAPIO_BUFFET)) || 
					(cardapioObjVO.getCategoriaCardapio().equals(constObj.CARDAPIO_SELFSERVICE))){
				try {

					System.out.println("ID BUFFET.................."+ cardapioObjVO.getIdCardapioBuffetWeb());
					
					FacesUtil.setApplicationMapValue("CardapiosMB.value", cardapioObjVO);
					FacesContext.getCurrentInstance().getExternalContext().redirect("../../user/cardapio/cardapioG1/altCardapioG1.jsf");
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else if((cardapioObjVO.getCategoriaCardapio().equals(constObj.CARDAPIO_LACARTE)) 
					|| (cardapioObjVO.getCategoriaCardapio().equals(constObj.CARDAPIO_INFANTIL))){
				try {
					FacesUtil.setApplicationMapValue("CardapiosMB.value", cardapioObjVO);
					FacesContext.getCurrentInstance().getExternalContext().redirect("../../user/cardapio/cardapioG2/altCardapioG2.jsf");
					
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else if ((cardapioObjVO.getCategoriaCardapio().equals(constObj.CARDAPIO_PIZZA)) || (cardapioObjVO.getCategoriaCardapio().equals(constObj.CARDAPIO_LANCHE))
					|| (cardapioObjVO.getCategoriaCardapio().equals(constObj.CARDAPIO_PETISCO)) || (cardapioObjVO.getCategoriaCardapio().equals(constObj.CARDAPIO_SALGADO))
					|| (cardapioObjVO.getCategoriaCardapio().equals(constObj.CARDAPIO_SOBREMESA))){
				try {
					FacesUtil.setApplicationMapValue("CardapiosMB.value", cardapioObjVO);
					FacesContext.getCurrentInstance().getExternalContext().redirect("../../user/cardapio/cardapioG3/altCardapioG3.jsf");
					
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}


		
// ======================================================================================================

		public CardapioG1Facade getCardapioG1EJB() {
			return cardapioG1EJB;
		}

		public void setCardapioG1EJB(CardapioG1Facade cardapioG1EJB) {
			this.cardapioG1EJB = cardapioG1EJB;
		}

		public CardapioG2Facade getCardapioG2EJB() {
			return cardapioG2EJB;
		}

		public void setCardapioG2EJB(CardapioG2Facade cardapioG2EJB) {
			this.cardapioG2EJB = cardapioG2EJB;
		}

		public CardapioG3Facade getCardapioG3EJB() {
			return cardapioG3EJB;
		}

		public void setCardapioG3EJB(CardapioG3Facade cardapioG3EJB) {
			this.cardapioG3EJB = cardapioG3EJB;
		}

		public LoginMB getLoginOjb() {
			return loginOjb;
		}

		public void setLoginOjb(LoginMB loginOjb) {
			this.loginOjb = loginOjb;
		}




		public boolean isStatusAlterar() {
			return statusAlterar;
		}




		public void setStatusAlterar(boolean statusAlterar) {
			this.statusAlterar = statusAlterar;
		}


		public CardapioVO getCardapioObjVO() {
			return cardapioObjVO;
		}




		public void setCardapioObjVO(CardapioVO cardapioObjVO) {
			this.cardapioObjVO = cardapioObjVO;
		}
}
